package cobersky.engine.media
{
	import cobersky.engine.core.IWaveViewer;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class WaveView extends Bitmap implements IWaveViewer
	{
		private var last:Number;
		private var bmd:BitmapData;
		public var radio:Number=0;
		private var _last:Number;
		public function WaveView(w:uint=512,h:uint=200){
			trace(231);
			_last=0;
			this.bitmapData=bmd=new BitmapData(w,h,false,0);
		}
		public function update(b:ByteArray):void{
			bmd.lock();
			bmd.scroll(0,-1);
			_last=0;
			bmd.applyFilter(bmd,bmd.rect,new Point,new BlurFilter(4,4));
			bmd.colorTransform(bmd.rect,new ColorTransform(1.005,.9,.9,1));
			for(var i:uint=0;i<512;i++){
				var f:Number=b.readFloat()*100;
				if(!(i&4)){
					_last+=f;
				}
				var n:int=f>>0;
				for(var j:uint=0;j<n;j++){
					const ii:int=(i<256)?256-i:i;
					bmd.setPixel(ii,199-j-Math.pow(256-ii,2)*.003,(j<<16)+0xffff);
				}
			}
			
			radio=Math.min(_last/last*10,30);
			trace(radio);
			last=_last;
			bmd.unlock();
		}
	}
}